﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;
using Demo.Frm;
using C1.Win.C1TrueDBGrid;

namespace Demo
{
    public partial class FrmEmployee : Form
    {
        //人员
        EmployeeModel _employeemodel = new EmployeeModel();
        EmployeeBLL _employeebll = new EmployeeBLL();

        //部门
        DepartmentModel _departmodel = new DepartmentModel();
        DepartmentBLL _deptbll = new DepartmentBLL();
        private string empNo = "";
        private string empName = "";
        public FrmEmployee()
        {
            InitializeComponent();
            Init();
        }        

        private void FrmEmployee_Load(object sender, EventArgs e)
        {
            LoadData("", "");
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            //设置复选框默认补选中
            this.txtEmployeeNo.ReadOnly = true;
            this.txtEmployeeName.ReadOnly = true;
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        public void InitGrid()
        {
            this.c1tdgEmployee.Columns.Clear();
            SetUnbandColumns(c1tdgEmployee, 0, "部门名称", "DepartmentId", 140, true);
            SetUnbandColumns(c1tdgEmployee, 1, "员工编码", "EmployeeNo", 140, true);
            SetUnbandColumns(c1tdgEmployee, 2, "员工名称", "EmployeeName", 140, true);
            SetUnbandColumns(c1tdgEmployee, 3, "员工性别", "EmployeeSex", 140, true);
            SetUnbandColumns(c1tdgEmployee, 4, "员工生日", "EmployeeBirth", 140, true);
            SetUnbandColumns(c1tdgEmployee, 5, "是否在职", "IsJob", 100, true);
            SetUnbandColumns(c1tdgEmployee, 6, "备注", "Remarks", 200, true);

            c1tdgEmployee.SetActiveCell(-1, 0);

        }

        public void SetUnbandColumns(C1TrueDBGrid grid, int index, string caption, string datafield, int width, bool visible)
        {
            C1DataColumn dataColumn = new C1DataColumn();
            grid.Columns.Insert(index, dataColumn);
            dataColumn.Caption = caption;
            dataColumn.DataField = datafield;

            C1DisplayColumn discolDeviceName = grid.Splits[0].DisplayColumns[dataColumn];
            grid.Splits[0].DisplayColumns.RemoveAt(index);
            grid.Splits[0].DisplayColumns.Insert(index, discolDeviceName);
            discolDeviceName.Visible = visible;
            if (width > 0)
                discolDeviceName.Width = width;
            else
                discolDeviceName.AutoSize();
            discolDeviceName.Style.HorizontalAlignment = AlignHorzEnum.Center;
            discolDeviceName.Style.VerticalAlignment = AlignVertEnum.Center;
            discolDeviceName.HeadingStyle.VerticalAlignment = AlignVertEnum.Center;
            discolDeviceName.HeadingStyle.HorizontalAlignment = AlignHorzEnum.Center;
            discolDeviceName.HeadingStyle.Borders.BorderType = BorderTypeEnum.Flat;
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnSearch_Click(object sender, EventArgs e)
        {
            GetQuery();
            LoadData(empNo, empName);
        }

        /// <summary>
        /// 新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnAdd_Click(object sender, EventArgs e)
        {
            FrmEmployeeAdd addfrm = new FrmEmployeeAdd();
            addfrm.Width = 606;
            addfrm.ShowDialog();
            if (addfrm.DialogResult == DialogResult.OK)
            {
                //重新加载数据
                LoadData("", "");
            } 
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnUpdate_Click(object sender, EventArgs e)
        {
            EmpUpdate();
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnDel_Click(object sender, EventArgs e)
        {
            EmpDelete();
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public void LoadData(string employeeNo, string employeeName)
        {
            IEnumerable<EmployeeModel> employeelist = _employeebll.GetModelListByNoAndName(employeeNo, employeeName);
            List<EmployeeModel> employeelistNew = new List<EmployeeModel>();
            foreach (var employee in employeelist)
            {
                _departmodel = _deptbll.Get(employee.DepartmentId);
                employee.DepartmentId = _departmodel.DepartmentName;                
                employeelistNew.Add(employee);
            }
            c1tdgEmployee.DataSource = employeelistNew;
            InitGrid();
        }

        /// <summary>
        /// 员工修改
        /// </summary>
        private void EmpUpdate()
        {
            if (c1tdgEmployee.SelectedRows.Count > 0)
            {
                EmployeeModel model = this.c1tdgEmployee[this.c1tdgEmployee.RowBookmark(this.c1tdgEmployee.Row)] as EmployeeModel;
                FrmEmployeeAdd updatefrm = new FrmEmployeeAdd(model.Id);
                updatefrm.Width = 606;
                updatefrm.ShowDialog();
                if (updatefrm.DialogResult == DialogResult.OK)
                {
                    //重新加载数据
                    LoadData("", "");
                } 
            }
            else
            {
                MessageBox.Show("请选择一条数据进行修改");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public void EmpDelete()
        {
            if (c1tdgEmployee.SelectedRows.Count > 0)
            {
                EmployeeModel model = this.c1tdgEmployee[this.c1tdgEmployee.RowBookmark(this.c1tdgEmployee.Row)] as EmployeeModel;
                //EmployeeModel model = c1tdgEmployee.CurrentRow.DataBoundItem as EmployeeModel;
                FrmEmployeeAdd updatefrm = new FrmEmployeeAdd(model.Id);
                string msg = "确定删除员工编号是" + model.EmployeeNo + "的这一项吗？";
                if ((int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != 1)
                {
                    return;
                }
                else
                {
                    _employeebll.Delete(model.Id);
                }                
            }
            else
            {
                MessageBox.Show("请选择一条数据进行修改");
            }
            GetQuery();
            LoadData(empNo, empName);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        public void GetQuery()
        {
            empNo = "";
            empName = "";
            if (this.cbEmployeeNo.Checked)
            {
                empNo = this.txtEmployeeNo.Text;
            }
            if (this.cbEmployeeName.Checked)
            {
                empName = this.txtEmployeeName.Text;
            }
        }

        /// <summary>
        /// cbEmployeeNo点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbEmployeeNo_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbEmployeeNo.Checked==false)
            {
                this.txtEmployeeNo.ReadOnly = true;
            }
            else
            {
                this.txtEmployeeNo.ReadOnly = false;
            }
        }

        /// <summary>
        /// cbEmployeeName点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbEmployeeName_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbEmployeeName.Checked==false)
            {
                this.txtEmployeeName.ReadOnly = true;
            }
            else
            {
                this.txtEmployeeName.ReadOnly = false;
            }
        }

        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void c1tdgEmployee_MouseClick(object sender, MouseEventArgs e)
        {
            int row = this.c1tdgEmployee.RowContaining(e.Y);
            if (row != -1)
            {
                int index = this.c1tdgEmployee.SelectedRows.IndexOf(row);
                if (index != -1)
                {
                    this.c1tdgEmployee.SelectedRows.RemoveAt(index);
                }
                else
                {
                    this.c1tdgEmployee.SelectedRows.Add(row);
                }
            }
        }
    }
}
